package com.qf.cardlikeboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cardlikeboot.entity.Card;
import com.qf.cardlikeboot.mapper.CardMapper;
import com.qf.cardlikeboot.service.CardService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 钻五大人
 * @date 2022/5/22 12:36
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CardServiceImpl implements CardService {

    @Resource
    CardMapper cardMapper;

    @Override
    public int addCard(Card card) {
        return cardMapper.insert(card);
    }

    //分页获取名片列表
    @Override
    public PageInfo<Card> selectCardByPage(Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Card> cards = cardMapper.selectAllCard();
        return new PageInfo<>(cards);
    }

    //喜欢的名片列表
    @Override
    public PageInfo<Card> selectCardByPageWithLiked(Integer pageIndex, Integer pageSize, List<String> liked) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Card> cards = cardMapper.selectByIdList(liked);
        return new PageInfo<>(cards);
    }

    @Override
    public Card showCard(Integer id) {
        // 查看名片,每次查看一次,浏览量加一
        Card card = cardMapper.selectByPrimaryKey(Long.valueOf(id));
        card.setVisited(card.getVisited() + 1);
        cardMapper.updateByPrimaryKeySelective(card);
        return null;
    }
}
